package eu.telecombretagne.rsm.gui.frames.panels;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JPanel;

import eu.telecombretagne.rsm.gui.NameConsts;
import eu.telecombretagne.rsm.gui.frames.objects.AutomateCircleState;
import eu.telecombretagne.rsm.gui.frames.objects.AutomateLegend;

public class AutomatePanel extends JPanel{
	
	private static final long serialVersionUID = 5653778549282978371L;
	
	private AutomateCircleState circleREPOS0;
	private AutomateCircleState circleATTENTE_ACK0;
	private AutomateCircleState circleREPOS1;
	private AutomateCircleState circleATTENTE_ACK1;
	
	private AutomateLegend legend;
	
	/**
	 * The constructor of the AutomatePanel.
	 * 
	 * @param pWidth A panel width value.
	 * @param pHeight A panel height value.
	 * @param pBackGroundColor A panel background color.
	 * @param pFirstColor A first color.
	 * @param pSecondColor A second color.
	 * @param pThirdColor A third color.
	 * @param pTitleColor A title color.
	 */
	public AutomatePanel(Integer pWidth, Integer pHeight, 
			Color pBackGroundColor, Color pFirstColor, Color pSecondColor, Color pThirdColor, Color pTitleColor){
		
		setBackground(pBackGroundColor);
		setPreferredSize(new Dimension(pWidth, pHeight));
		
		initAutomataStateCircles(pWidth, pHeight, 50, 100, 
				pFirstColor, pSecondColor, new Color(235, 235, 235), pTitleColor);
		
		legend = new AutomateLegend(0, pHeight-15, NameConsts.AUTOMATEEMITTER, NameConsts.AUTOMATERECEIVER, pTitleColor, pFirstColor, pSecondColor);
	}
	
	/**
	 * Performs a drawing action.
	 * @param g A Graphics object.
	 */
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		
		circleREPOS0.draw(g);
		circleREPOS1.draw(g);
		circleATTENTE_ACK0.draw(g);
		circleATTENTE_ACK1.draw(g);
		
		legend.draw(g);
	}
	
	private void initAutomataStateCircles(Integer pWidth, Integer pHeight, Integer pRadius, Integer pMargin, 
			Color pFirstColor, Color pSecondColor, Color pThirdColor, Color pTitleColor){
		circleREPOS0 = new AutomateCircleState(0 + pMargin, 0 + pMargin, pRadius,
				pFirstColor, pSecondColor, pThirdColor, pTitleColor, NameConsts.REPOS0);
		circleREPOS1 = new AutomateCircleState(pWidth - pMargin, pHeight - pMargin, pRadius, 
				pFirstColor, pSecondColor, pThirdColor, pTitleColor, NameConsts.REPOS1);
		circleATTENTE_ACK0 = new AutomateCircleState(pWidth - pMargin, 0 + pMargin, pRadius, 
				pFirstColor, pSecondColor, pThirdColor, pTitleColor, NameConsts.ATTENTEACK0);
		circleATTENTE_ACK1 = new AutomateCircleState(0 + pMargin, pHeight - pMargin, pRadius, 
				pFirstColor, pSecondColor, pThirdColor, pTitleColor, NameConsts.ATTENTEACK1);
	}
	
	/**
	 * Sets the receiver automate state.
	 * @param pREPOS0 If set to <i>true</i> REPOS0 state is set.
	 * @param pREPOS1 If set to <i>true</i> REPOS1 state is set.
	 * @param pACK0 If set to <i>true</i> ACK0 state is set.
	 * @param pACK1 If set to <i>true</i> ACK1 state is set.
	 */
	public void setReceiver(Boolean pREPOS0, Boolean pREPOS1, Boolean pACK0, Boolean pACK1){
		setReceiverRingVisible(circleREPOS0, pREPOS0);
		setReceiverRingVisible(circleREPOS1, pREPOS1);
		setReceiverRingVisible(circleATTENTE_ACK0, pACK0);
		setReceiverRingVisible(circleATTENTE_ACK1, pACK1);
	}
	
	/**
	 * Sets the emitter automate state.
	 * @param pREPOS0 If set to <i>true</i> REPOS0 state is set.
	 * @param pREPOS1 If set to <i>true</i> REPOS1 state is set.
	 * @param pACK0 If set to <i>true</i> ACK0 state is set.
	 * @param pACK1 If set to <i>true</i> ACK1 state is set.
	 */
	public void setEmitter(Boolean pREPOS0, Boolean pREPOS1, Boolean pACK0, Boolean pACK1){
		setEmitterRingVisible(circleREPOS0, pREPOS0);
		setEmitterRingVisible(circleREPOS1, pREPOS1);
		setEmitterRingVisible(circleATTENTE_ACK0, pACK0);
		setEmitterRingVisible(circleATTENTE_ACK1, pACK1);
	}
	
	private void setEmitterRingVisible(AutomateCircleState pAutomateCircleState, Boolean pVisible){
		pAutomateCircleState.setFirstRingVisible(pVisible);
	}
	
	private void setReceiverRingVisible(AutomateCircleState pAutomateCircleState, Boolean pVisible){
		pAutomateCircleState.setSecondRingVisible(pVisible);
	}
}